package mygame;

import com.jme3.input.InputManager;
import com.jme3.input.Joystick;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.JoyAxisTrigger;
import com.jme3.input.controls.KeyTrigger;

/**
 * Static class and Subclasses that handle controlling the game.
 * @author Matthias
 */
public class Controls {

	public void initKeyMappings() {
		InputManager inputManager = Game.getInstance().getInputManager();
		// clear mappings first: don't want default camera controls
		inputManager.clearMappings();
		// check for joysticks. Debug stuff.
		Joystick[] joysticks = inputManager.getJoysticks();
		// Matthi, das stürzt ab  wenn wir keiner Joysticks haben :(
//		System.out.println("Joystick Detected (Not Necessarily Supported): " + joysticks[0].getName());
		// add ESC as exit button (useful when testing fullscreen)
		inputManager.addMapping("Exit", new KeyTrigger(KeyInput.KEY_ESCAPE));
		// add steering.
		inputManager.addMapping("SteerLeft", new JoyAxisTrigger(0, 1, true));
		inputManager.addMapping("SteerRight", new JoyAxisTrigger(0, 1, false));
		// add listeners to those events
		inputManager.addListener(escapeListener, new String[]{"Exit"});
		inputManager.addListener(steeringListener, new String[]{"SteerLeft", "SteerRight"});
	}

	// listener for esc
	private ActionListener escapeListener = new ActionListener() {

		public void onAction(String name, boolean isPressed, float tpf) {
			if (name.equals("Exit") && !isPressed) {
				// stop running game instance
				Game.getInstance().stop();
			}
		}
	};

	// listener for steering
	private AnalogListener steeringListener = new AnalogListener() {

		public void onAnalog(String name, float isPressed, float tpf) {
			// TODO: Add real steering here.
			// This code is just saying that it was steering.
			// Which is a lie. It doesn't steer. At all.
			if (name.equals("SteerLeft")) {
				System.out.println("Steeing left: " + isPressed);
			}
			if (name.equals("SteerRight")) {
				System.out.println("Steeing right: " + isPressed);
			}
		}
	};
}
